package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.common.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * @ClassName CheckGroupController
 * @Description: TODO
 * @Author 郑乾
 * @Date 2020-06-21
 * @Version V1.0
 **/
@RestController
@RequestMapping("/checkgroup")
@Slf4j
public class CheckGroupController {
    @Reference
    private CheckGroupService checkGroupService;

    @RequestMapping("add")
    public Result add(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds) {
        log.debug("checkGroup...." + checkGroup + "....checkitemIds...." + checkitemIds);
        checkGroupService.add(checkGroup, checkitemIds);
        log.debug("添加成功");
        return new Result(true, MessageConst.ADD_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("findByPage")
    public PageResult findByPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = checkGroupService.findByPage(queryPageBean);
        log.debug("查询成功...." + pageResult);
        return pageResult;
    }

    @RequestMapping("findById")
    public Result findById(Integer id) {
        CheckGroup checkGroup = checkGroupService.findById(id);
        log.debug("checkGroup...." + checkGroup);
        return new Result(true, MessageConst.QUERY_CHECKGROUP_SUCCESS, checkGroup);
    }

    @RequestMapping("findCheckItemIdsByCheckGroup")
    public Result findCheckItemIdsByCheckGroup(Integer id) {
        List<Integer> checkItemIdsList = checkGroupService.findCheckItemIdsByCheckGroup(id);
        log.debug("checkItemIdsList...." + checkItemIdsList);
        return new Result(true, MessageConst.QUERY_CHECKITEM_SUCCESS, checkItemIdsList);
    }

    @RequestMapping("edit")
    public Result edit(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds) {
        checkGroupService.edit(checkGroup, checkitemIds);
        log.debug("检查项集合....." + Arrays.toString(checkitemIds));
        return new Result(true, MessageConst.EDIT_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("delById")
    public Result delById(Integer id) {
        checkGroupService.delById(id);
        return new Result(true, MessageConst.DELETE_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("findAll")
    public Result findAll() {
        List<CheckGroup> checkGroupList = checkGroupService.findAll();
        return new Result(true, MessageConst.QUERY_CHECKGROUP_SUCCESS, checkGroupList);
    }
}
